import { PaginateOptionDTO } from '@app/models/paginate.model'
import {
  ArrayNotEmpty,
  IsArray,
  IsDefined,
  IsIn,
  IsInt,
  IsNotEmpty,
  IsObject,
  IsOptional,
  ValidateNested,
} from 'class-validator'
import { VOTE_AUTHOR_TYPES, VOTE_TARGETS, VOTE_TYPES, VoteType } from './vote.model'
import { Transform } from 'class-transformer'
import { unknownToNumber } from '@app/transformers/value.transformer'
import { Author } from '../comment/comment.model'

export class VotePaginateQueryDTO extends PaginateOptionDTO {
  @IsIn(VOTE_TARGETS)
  @IsInt()
  @IsNotEmpty()
  @IsOptional()
  @Transform(({ value }) => unknownToNumber(value))
  target_type?: number

  @IsInt()
  @IsNotEmpty()
  @IsOptional()
  @Transform(({ value }) => unknownToNumber(value))
  target_id?: number

  @IsIn(VOTE_TYPES)
  @IsInt()
  @IsNotEmpty()
  @IsOptional()
  @Transform(({ value }) => unknownToNumber(value))
  vote_type?: number

  @IsIn(VOTE_AUTHOR_TYPES)
  @IsInt()
  @IsNotEmpty()
  @IsOptional()
  @Transform(({ value }) => unknownToNumber(value))
  author_type?: number
}

export class VotesDTO {
  @IsArray()
  @ArrayNotEmpty()
  vote_ids: string[]
}

export class VoteAuthorDTO {
  @ValidateNested()
  @IsObject()
  @IsOptional()
  author?: Author
}

export class CommentVoteDTO extends VoteAuthorDTO {
  @IsInt()
  @IsDefined()
  comment_id: number

  @IsIn(VOTE_TYPES)
  @IsInt()
  @IsDefined()
  vote: number
}

export class PostVoteDTO extends VoteAuthorDTO {
  @IsInt()
  @IsDefined()
  post_id: number

  @IsIn([VoteType.Upvote])
  @IsInt()
  @IsDefined()
  vote: number
}
